home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / tn3270 / saio.c < prev    next >
C/C++ Source or Header  |  1992-04-17  |  12KB  |  483 lines

  1. /*
  2.  *  tn3270 for the Macintosh Source Code
  3.  *  Brown University Computing and Information Services
  4.  *  Version 2.4d7  April, 1992
  5.  *  Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
  6.  *  Peter John DiCamillo.
  7.  *
  8.  *  Permission is granted to any individual or institution to use, copy,
  9.  *  or redistribute the binary version of this software and its
  10.  *  documentation provided this notice and the copyright notices are
  11.  *  retained.  Permission is granted to any individual or non-profit
  12.  *  institution to use, copy, modify, or redistribute the source files
  13.  *  of this software provided this notice and the copyright notices are
  14.  *  retained.  This software may not be distributed for profit, either
  15.  *  in original form or in derivative works, nor can the source be
  16.  *  distributed to other than an individual or a non-profit institution.
  17.  *  Any  individual or group interested in seeing and/or using these
  18.  *  source files but who are prevented from doing so by the above
  19.  *  constraints should contact Don Wolfe, Assistant Vice-President for
  20.  *  Computer Systems at Brown University, (401) 863-7250, for possible
  21.  *  software licensing of the source developed at Brown.
  22.  *
  23.  *  Brown University and Peter John DiCamillo make no representations
  24.  *  about the suitability of this software for any purpose.
  25.  *
  26.  *  BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
  27.  *  EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
  28.  *  INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
  29.  *  WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  */
  32.  
  33. #if !defined(USEDUMP)
  34.     #include "maclib.h"
  35.     #include "termdef.h"
  36.     #include "tn3270funcs.h"
  37.     #include "globals.h"
  38. #else
  39.     #pragma load "tn3270DumpFile"
  40. #endif
  41.  
  42. #pragma segment 3270sa
  43.  
  44. extern main();                /* for debugging */
  45.  
  46. unsigned char *sarcvbuff;
  47.  
  48. OSErr sa_init(void)
  49. {
  50. Handle version;
  51. short i;
  52.  
  53. putln("sa_init called");
  54.                                         /* version info. for debugging */
  55. strcpy(dbgmsg, "Version string: ");
  56. version = GetResource('GFTM', 0);
  57. if (version != 0) {
  58.     i = strlen(dbgmsg);
  59.     memcpy(dbgmsg+i, (*version)+1, (*version)[0]);
  60.     dbgmsg[i+(*version)[0]] = 0;
  61.     }
  62. else strcpy(dbgmsg, "Version string not available.");
  63. putln(dbgmsg);
  64. strcpy(dbgmsg, "VM file transfer version: ");
  65. version = GetResource('WSFR', 161);
  66. if (version != 0) {
  67.     i = strlen(dbgmsg);
  68.     memcpy(dbgmsg+i, (*version)+13, (*version)[12]-1);
  69.     dbgmsg[i+(*version)[12]-1] = 0;
  70.     while (dbgmsg[i] != 0) {
  71.         dbgmsg[i] = ((unsigned char *)(*xtabh))[dbgmsg[i]];
  72.         i++;
  73.         }
  74.     }
  75. else strcpy(dbgmsg, "VM file transfer version unknown.");
  76. putln(dbgmsg);
  77. sprintf(dbgmsg, "main at %lx", main);    /* in case we're using a map */
  78. putln(dbgmsg);                            /* for debugging */
  79.  
  80. /* allocate receive buffers */
  81. sarcvbuff = (unsigned char *)NewPtr(614L);    /* 550 + 64 */
  82. if (sarcvbuff == 0L) {
  83.     stgalert("sa rcv buffer", "NewPtr", 614L);
  84.     return(-1);
  85.     }
  86. return(0);
  87. }
  88.  
  89. void sadflthost(unsigned char *s)
  90. {
  91. strcpy(s, "?");            /* If "?", prompt will be issued */
  92. }
  93.  
  94. long sagetrsrv(cnr *cp)
  95. {
  96. #pragma unused(cp)
  97. return(0);
  98. }
  99.  
  100. void salgin(cnr *cp)
  101. {
  102. OSErr rc;
  103.  
  104. login_init(cp);
  105. cp->connstate = 4;
  106. cp->online = 1;
  107. cp->kblcode = 2;
  108. if (cp->myWindow == 0) {
  109.     rc = createwindow(cp);
  110.     if (rc != noErr) {
  111.         salgout(0, cp);
  112.         }
  113.     }
  114. else {
  115.     newstat(cp);
  116.     }
  117. /*     if the session was opened by the api, we
  118.     delay registering the session until data
  119.     is received. (See telnet.c)  */
  120. if (!(cp->apiopenpend)) {
  121.     apiregister(1, cp);
  122.     }
  123. if (cp->serverconn) {
  124.     cp->servermode = 0;
  125.     srvswitch(cp);
  126.     if (!(cp->logon)) {
  127.         if (cp->kblock) {
  128.             cp->kblock = 0;
  129.             newstat(cp);
  130.             }
  131.         cp->hadascii = 1;
  132.         return;
  133.         }
  134.     }
  135. sendinit(0xc7, cp);
  136. }
  137.  
  138. void sendinit(unsigned char wcc, cnr *cp)
  139. {
  140. static unsigned char initdata[] = 
  141.     {0x01, 0xc7, 0x11, 0x5d, 0x6b, 0x1d, 0x60, 0xd9, 0xe4,
  142.      0xd5, 0xd5, 0xc9, 0xd5, 0xc7, 0x40, 0x40, 0x40, 0xc2,
  143.      0xd9, 0xd6, 0xe6, 0xd5, 0xe5, 0xd4, 0x40, 0x40, 0x1d,
  144.      0x40};
  145.  
  146. initdata[1] = wcc;
  147. memcpy(sarcvbuff+64, initdata, sizeof(initdata));
  148. if (cp->cs.dblevel > 2)
  149.     dbgdump("sendinit", sarcvbuff+64, sizeof(initdata));
  150. sadata(sarcvbuff+64, sizeof(initdata), 1, 1, cp);
  151. if (cp->wr_active && (!(cp->needwrite))) {
  152.     cp->wr_active = 0;
  153.     newstat(cp);
  154.     }
  155. if (cp->fsinv && (cp->data_init == 1)) {    /* 3270 mode */
  156.     cp->fsinv = 0;
  157.     invldscr(cp);
  158.     if (cp->pndbeep) {
  159.         cp->pndbeep = 0;
  160.         beep(cp);
  161.         }
  162.     }
  163. else if (cp->logon == 0) {            /* line mode */
  164.     if (cp->pndbeep) {
  165.         cp->pndbeep = 0;
  166.         beep(cp);
  167.         }
  168.     }
  169. }
  170.  
  171. void salgout(char keep, cnr *cp)
  172. {
  173. sessionlgout(keep, cp);
  174. }
  175.  
  176. void sa_end(void)
  177. {
  178. putln("sa_end called");
  179.  
  180. if (sarcvbuff != 0) {
  181.     DisposPtr(sarcvbuff);
  182.     sarcvbuff = 0;
  183.     }
  184. }
  185.  
  186. void sawrite(unsigned char *dptr, short dsize, cnr *cp)
  187. {
  188. cp->online = 0;
  189. newstat(cp);
  190. if (cp->cs.dblevel > 2) dbgdump("sawrite", dptr, dsize);
  191. cp->online = 1;
  192. newstat(cp);
  193. sendinit(0xc3, cp);
  194. }
  195.  
  196. void sain(unsigned char asc, unsigned char chr, unsigned char shift, cnr *cp)
  197. {
  198.  
  199. if (!(cp->logon)) {                /* Return or CLEAR for new session, else beep */
  200.     if (cp->connstate == 0) kbnew(chr, cp);
  201.     else if ((cp->connstate == 4) && (!(cp->kblock)))
  202.                 sakbin(asc, chr, shift, cp);
  203.     else beep(cp);
  204.     return;
  205.     }
  206.  
  207. if (chr == 196) {            /* perform program reset immediately */
  208.     sa_reset(cp);
  209.     xfrst(cp);
  210.     invldscr(cp);
  211.     return;
  212.     }
  213.  
  214. if (chr == 185) {            /* perform keyboard immediately */
  215.     hndkbd(chr, shift, cp);
  216.     return;
  217.     }
  218.  
  219. if (chr == 197) {            /* perform print screen immediately */
  220.     prscreen(cp);
  221.     return;
  222.     }
  223.  
  224. if ((cp->kbqsize == KBQMAX) || (cp->vmxbgn && (!(cp->vmxsub)))) {
  225.     beep(cp);            /* error if xfer mode or queue full */
  226.     kerr(3, cp);
  227.     }
  228. else {                        /* else add to keyboard queue */
  229.     cp->kbqueue[cp->kbqsize].code = chr;
  230.     cp->kbqueue[cp->kbqsize].shift = shift;
  231.     cp->kbqsize++;
  232.     }
  233. }
  234.  
  235. void sa_reset(cnr *cp)
  236. {
  237. if (cp->logon) {
  238.     cp->kblock = cp->kblcode = 0;
  239.     cp->online = 1;
  240.     }
  241. cp->lastwcc = 0x02;
  242. cp->kb_err = 0;
  243. cp->kbqsize = 0;
  244. cp->aplmode = cp->insmode = 0;
  245. cp->fixbracket = cp->cs.std_brack && (!(cp->aplmode)) &&
  246.     (cp->stdfont != ALAFONT);
  247. newstat(cp);
  248. }
  249.  
  250. void saevent(void)
  251. {
  252. static unsigned long * Ticks = (unsigned long *)0x16a;
  253. short i, j;
  254. cnr *cp;
  255. short kindex;
  256. unsigned long klimit;
  257. CursHandle watchHandle;
  258. char waitcur;
  259.  
  260. for (j=0; j < MAXSESSIONS; j++) {
  261.     cp = cplist[j];
  262.     if (cp != 0) {
  263.         if ((cp->serflg == 0) && (cp->tcpflg == 0)) {
  264.             kindex = 0;
  265.             klimit = (*Ticks) + 15;
  266.             waitcur = 0;
  267.             if (cp->pasteactive == 0) {
  268.                 while ((cp->kbqsize > 0) && (!(cp->kblock))) {
  269.                     if (cp->vmxbgn && (!(cp->vmxsub))) break;
  270.                     if (cp->pasteactive) break;
  271.                     if (((cp->kbqueue)[kindex].shift) == 100) {
  272.                         cp->pasteactive = 1;
  273.                         }
  274.                     else {
  275.                         hndkbd((cp->kbqueue)[kindex].code,
  276.                                (cp->kbqueue)[kindex].shift, cp);
  277.                         }
  278.                     kindex++;
  279.                     cp->kbqsize--;
  280.                     if ((*Ticks) > klimit) {
  281.                         klimit = (*Ticks) + 15;
  282.                         if (waitcur == 0) {
  283.                             watchHandle = GetCursor(4);
  284.                             SetCursor(*watchHandle);
  285.                             waitcur = 1;
  286.                             dfltcurs = 2;
  287.                             }
  288.                         }
  289.                     }
  290.                 if ((cp->kbqsize > 0) && (kindex > 0)) {
  291.                     for (i = 0; i < cp->kbqsize; i++) {
  292.                         (cp->kbqueue)[i].code = (cp->kbqueue)[i+kindex].code;
  293.                         (cp->kbqueue)[i].shift = (cp->kbqueue)[i+kindex].shift;
  294.                         }
  295.                     }
  296.                 }
  297.             else {        /* pasteactive true */
  298.                 while ((cp->pastebuffsize > 0) && (!(cp->kblock))) {
  299.                     if (cp->vmxbgn && (!(cp->vmxsub))) break;
  300.                     hndkbd((cp->pastebuff)[kindex], 99, cp);
  301.                     kindex++;
  302.                     cp->pastebuffsize--;
  303.                     if ((*Ticks) > klimit) {
  304.                         klimit = (*Ticks) + 15;
  305.                         if (waitcur == 0) {
  306.                             watchHandle = GetCursor(4);
  307.                             SetCursor(*watchHandle);
  308.                             waitcur = 1;
  309.                             dfltcurs = 2;
  310.                             }
  311.                         }
  312.                     }
  313.                 if ((cp->pastebuffsize > 0) && (kindex > 0)) {
  314.                     for (i = 0; i < cp->pastebuffsize; i++) {
  315.                         (cp->pastebuff)[i] = (cp->pastebuff)[i+kindex];
  316.                         }
  317.                     }
  318.                 if (cp->pastebuffsize == 0) {
  319.                     cp->pasteactive = 0;
  320.                     DisposPtr(cp->pastebuff);
  321.                     cp->pastebuff = 0;
  322.                     if (cp->pastebeep) beep(cp);
  323.                     newwrite(cp);
  324.                     newcur(cp);
  325.                     }
  326.                 }
  327.             }
  328.         }
  329.     }
  330. }
  331.  
  332. void sadata(unsigned char *sptr, short slen,
  333.             char init, char end, cnr *cp)
  334. {
  335. short wsflen;
  336. unsigned char * dataptr;
  337. short datalen;
  338. char winit;
  339.  
  340. if (cp->cs.dblevel > 2) {
  341.     sprintf(dbgmsg, "sadata: len = %d, init = %d, end = %d",
  342.                     slen, init, end);
  343.     putln(dbgmsg);
  344.     }
  345.  
  346. if (cp->apiopenpend) {
  347.     apiregister(1, cp);            /* register session */
  348.     if (!(cp->logon)) {
  349.         apiopenresp(openLineMode, cp);
  350.         }
  351.     else {
  352.         apiopenresp(open3270Mode, cp);
  353.         }
  354.     }
  355.     
  356. if (!(cp->logon)) {
  357.     cp->kblock = cp->kblcode = 0;
  358.     cp->showfkey = 1;        /* enable fkey menu */
  359.     appl_menu();
  360.     cp->connstate = 5;
  361.     cp->logon = 1;
  362.     newstat(cp);
  363.     }
  364.  
  365. /* length 0 valid only if init==0 and end==1 */
  366. if ((slen == 0) && (init || (!end))) return;
  367.  
  368. if (init) {
  369.     cp->wsfflag = ((sptr[0] == 0x11) || (sptr[0] == 0xf3)); 
  370.     if (cp->wsfflag) {
  371.         cp->wsfptr = cp->wsfbuff;
  372.         cp->wsfleft = 8192;
  373.         cp->wsfop = sptr[0];
  374.         }
  375.     }
  376.  
  377. if (cp->wsfflag) {
  378.     if (slen > 0) {
  379.         if (slen > cp->wsfleft) wsflen = cp->wsfleft;
  380.         else wsflen = slen;
  381.         memcpy(cp->wsfptr, sptr, wsflen);
  382.         cp->wsfleft -= wsflen;
  383.         cp->wsfptr += wsflen;
  384.         }
  385.     if (end) { 
  386.         if (cp->cs.dblevel > 1) dbgdump("writesf", cp->wsfbuff,
  387.             cp->wsfptr-cp->wsfbuff);
  388.         writesf(cp->wsfbuff, cp->wsfptr-cp->wsfbuff, 1, cp);
  389.         writeresponse(cp->wsfop, cp);
  390.         }
  391.     return;
  392.     }
  393.  
  394. if (init) {
  395.     cp->lstopcd = sptr[0];
  396.     cp->xtralen = cp->xtrainit = 0;
  397.     cp->needtoend = 0;
  398.     }
  399.  
  400. switch (cp->lstopcd) {
  401.     case 0x01:                    /* write */
  402.     case 0x05:                    /* erase/write */
  403.     case 0x0d:                    /* erase/write alt. */
  404.     case 0xf1:                    /* SNA write */
  405.     case 0xf5:                    /* SNA erase/write */
  406.     case 0x7e:                    /* SNA erase/write alt. */
  407.             if (init && (slen == 1)) {        /* just op-code */
  408.                 cp->xtralen = 1;            /* save op-code until wcc */
  409.                 cp->xtradata[0] = sptr[0];
  410.                 cp->xtrainit = 1;
  411.                 break;
  412.                 }
  413.             if (slen > 0) {
  414.                 cp->needtoend = 1;
  415.                 if (cp->xtralen > 0)
  416.                     memcpy(sptr-cp->xtralen, cp->xtradata, cp->xtralen);
  417.                 cp->ldvoff = 0;
  418.                 dataptr = sptr-cp->xtralen;
  419.                 datalen = slen + cp->xtralen;
  420.                 if (cp->xtrainit) winit = 1;
  421.                 else winit = init;
  422.                 if (cp->cs.dblevel > 1) {
  423.                     sprintf(dbgmsg, "writetm: init = %d", winit);
  424.                     dbgdump(dbgmsg, dataptr, datalen);
  425.                     }
  426.                 writetm(dataptr, datalen, winit, cp);
  427.                 cp->xtralen = datalen - cp->ldvoff;
  428.                 cp->xtrainit = 0;
  429.                 if (cp->xtralen == 0) break;
  430.                 if (cp->xtralen > 64) cp->xtralen = 64;
  431.                 memcpy(cp->xtradata, dataptr+cp->ldvoff, cp->xtralen);
  432.                 }
  433.             break;
  434.     case 0x0f:                    /* erase all unprot. */
  435.     case 0x6f:                    /* SNA erase all unprot. */
  436.             if (!init) return;
  437.             funckey(10, 0, cp);
  438.             cp->rdaid = 60;
  439.             writeresponse(cp->lstopcd, cp);
  440.             return;
  441.     case 0x06:                    /* read modified */
  442.     case 0xf6:                    /* SNA read modified */
  443.             if (!init) return;
  444.             rdmcmd(cp->rdaid, cp);
  445.             writeresponse(cp->lstopcd, cp);
  446.             return;
  447.     case 0x6e:                    /* SNA read modified all */
  448.             if (!init) return;
  449.             rdmacmd(cp->rdaid, cp);
  450.             writeresponse(cp->lstopcd, cp);
  451.             return;
  452.     case 0x02:                    /* read buffer */
  453.     case 0xf2:                    /* SNA read buffer */
  454.             if (!init) return;
  455.             rdbuff(cp);
  456.             writeresponse(cp->lstopcd, cp);
  457.             return;
  458.     default:
  459.             return;
  460.     }
  461.  
  462. if (end) {
  463.     if (cp->needtoend) {
  464.         endwcc(cp);    /* sets pndbeep if beep needed */
  465.         writeresponse(cp->lstopcd, cp);
  466.         cp->needtoend = 0;
  467.         }
  468.     cp->fsinv = 1;        /* saevent will call invldscr and possibly beep */
  469.     }
  470. }
  471.  
  472. void sakbin(unsigned char asc, unsigned char chr,
  473.              unsigned char shift, cnr *cp)    
  474. {                                /* handle new character in line mode */
  475. #pragma unused(chr, shift)
  476.  
  477. unsigned char c;
  478.  
  479. if (asc > 127) return;
  480. c = asc;
  481. sawrite(&c, 1, cp);
  482. }
  483.